package cn.wowjoy.pda.android_pharmacy.mvvm.presenter;

import android.content.Intent;
import android.util.Log;

import java.io.Serializable;
import java.util.List;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.doorPatientInfo.DoorPatientInfoBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.doorPatientInfo.DoorPatientNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.pendingTextInfo.PendingTextNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.quey.QueryBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.quey.QueryNode;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.DoorTakeIndexViewModel;
import cn.wowjoy.pda.android_pharmacy.ui.activity.door.take.DoorTakeMedicineDetailActivity;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by sinyo on 2017/7/18.
 */

public class DoorTakeIndexPresenter extends BasePresenter<DoorTakeIndexViewModel> {
    private ApiService mApiService;
    private MDialog waitDialog;
    @Override
    public void onCreatePresenter() {
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);
     }

     public void loadData(final String DJJLID){
         //TODO:联网获取数据
         Disposable disposable = null;
         disposable = mApiService.doXml(XmlUtils.prepare(DoorPatientNode.getContent("PDAQ01",DJJLID)))
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
                 .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                     @Override
                     protected void onStart() {
                         if(null == waitDialog){
                             waitDialog = CreateDialog.waitingDialog(activity);
                         }
                     }

                     @Override
                     public void onNext(RoleInfoResponseEnvelope xmlData) {
                         CreateDialog.dismiss(activity);
                         //TODO:门诊信息获取失败 不跳转并提示重新扫码
                         if(!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)){
                             String erroMsg = XmlUtils.getErroMsg(xmlData.body.roleInfoResponse.retXml);
                             if(null != erroMsg){
                                 new MyToast(activity).showinfo(erroMsg);
                             }else{
                                 new MyToast(activity).showinfo("信息获取失败,请确认单号");
                             }
                             return;
                         }
                         //朱一新  00026
                         //TODO:门诊信息获取正确 跳转并传值
                         List<DoorPatientInfoBeen> employeeInfoBeenList = XmlUtils.getDoorPatientInfoBeenList(xmlData.body.roleInfoResponse.retXml);
                         //设置拼音，查询
                         if(null != employeeInfoBeenList && employeeInfoBeenList.size()>0){
                             Intent intent=new Intent(activity, DoorTakeMedicineDetailActivity.class);
                             intent.putExtra("doorPatientInfoBeen",(Serializable) employeeInfoBeenList);
                             intent.putExtra("DJJLID",DJJLID);
                             intent.putExtra("BRDAID","null");
                             intent.putExtra("BRZYID","0");
                             activity.startActivity(intent);
                         }else{
                             new MyToast(activity).showinfo("单据异常");
                         }

                     }
                     @Override
                     public void onComplete() {
                     }
                     @Override
                     public void onError(Throwable e) {
                         CreateDialog.dismiss(activity);
                         if(null != e && null != e.getMessage()){
                             Log.e("Throwable", e.getMessage());
                             new MyToast(activity).showinfo(e.getMessage());
                         }
                     }
                 });
         addDisposable(disposable);
     }
    public void loadDataCY(final String DJJLID, final String BRDAID){
        //TODO:联网获取数据
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(DoorPatientNode.getCYContent("PDAQ12",DJJLID,BRDAID)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        //TODO:门诊信息获取失败 不跳转并提示重新扫码
                        if(!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)){
                            String erroMsg = XmlUtils.getErroMsg(xmlData.body.roleInfoResponse.retXml);
                            if(null != erroMsg){
                                new MyToast(activity).showinfo(erroMsg);
                            }else{
                                new MyToast(activity).showinfo("信息获取失败,请确认单号");
                            }
                            return;
                        }
                        //朱一新  00026
                        //TODO:门诊信息获取正确 跳转并传值
                        List<DoorPatientInfoBeen> employeeInfoBeenList = XmlUtils.getDoorPatientInfoBeenList(xmlData.body.roleInfoResponse.retXml);
                        if(null != employeeInfoBeenList && employeeInfoBeenList.size()>0){
                            Intent intent=new Intent(activity, DoorTakeMedicineDetailActivity.class);
                            intent.putExtra("doorPatientInfoBeen",(Serializable) employeeInfoBeenList);
                            intent.putExtra("DJJLID",DJJLID);
                            intent.putExtra("BRDAID",BRDAID);
                            intent.putExtra("BRZYID",employeeInfoBeenList.get(0).getBRZYID());
                            activity.startActivity(intent);
                        }else{
                            new MyToast(activity).showinfo("单据异常");
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    public void quey(String CXDJLX, final String DJJLID){
        //TODO:联网获取数据
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(QueryNode.getContent("PDAQ09",CXDJLX,DJJLID)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                            if(null == waitDialog){
                                waitDialog = CreateDialog.waitingDialog(activity);
                            }
                    }
                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        QueryBeen queryBeen = XmlUtils.getQueryBeen(xmlData.body.roleInfoResponse.retXml);
//                        Log.e(TAG, "apply: "+queryBeen.toString() );
                        if( null != queryBeen){
                            if(Contants.FIRST.equals(queryBeen.getDJDQZT())){
                                loadData(DJJLID);
                                return;
                            }
                            switch (queryBeen.getDJDQZT()){
                                case Contants.DOOR_TAKE:
                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已取,请重新确认");
                                    break;
                                case Contants.DOOR_CHECK:
                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已复核,请重新确认");
                                    break;
                                case Contants.SEND:
                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已送出,请重新确认");
                                    break;
                                case Contants.RECEIVER:
                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已接收,请重新确认");
                                    break;
                                case Contants.ERROR:
                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为异常,请重新确认");
                                    break;
                            }
                        }


                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    public void queyCY(String CXDJLX, final String DJJLID, final String BRDAID){
        //TODO:联网获取数据
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(QueryNode.getCYContent("PDAQ09",CXDJLX,Long.parseLong((DJJLID+BRDAID).trim()))))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }
                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        QueryBeen queryBeen = XmlUtils.getQueryBeen(xmlData.body.roleInfoResponse.retXml);
//                        Log.e(TAG, "apply: "+queryBeen.toString() );
                        if( null != queryBeen){
                            if(Contants.FIRST.equals(queryBeen.getDJDQZT())){
                                loadDataCY(DJJLID,BRDAID);
                                return;
                            }
                            switch (queryBeen.getDJDQZT()){
                                case Contants.HOSPITAL_STATIC_TAKE:
                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已取,请重新确认");
                                    break;
                                case Contants.HOSPITAL_STATIC_CHECK:
                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已复核,请重新确认");
                                    break;
                                case Contants.SEND:
                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已送出,请重新确认");
                                    break;
                                case Contants.RECEIVER:
                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为已接收,请重新确认");
                                    break;
                                case Contants.ERROR:
                                    CreateDialog.failDialog3(activity,"无法进行取药","此取药单当前状态为异常,请重新确认");
                                    break;
                            }
                        }


                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    /**
     * 1 门诊当日未取药处方数
       2 住院取完药 待审核数量
     * @param CXFSLX
     */
     public void setPendingNum(String CXFSLX){
         Disposable disposable = null;
         disposable = mApiService.doXml(XmlUtils.prepare(PendingTextNode.getContent("PDAQ07",CXFSLX)))
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
                 .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                     @Override
                     protected void onStart() {
                         if(null == waitDialog){
                             waitDialog = CreateDialog.waitingDialog(activity);
                         }
                     }

                     @Override
                     public void onNext(RoleInfoResponseEnvelope xmlData) {
                         CreateDialog.dismiss(activity);
                         //TODO:获取待完成订单
                         if(!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)){
//                             new MyToast(activity).showinfo("未取药信息获取失败");
                             return;
                         }
                         //TODO:当日未取药处方数 / 住院取完药，待审核的数量     PDAQ07
                         String pendingNum =XmlUtils.getPendingNum(xmlData.body.roleInfoResponse.retXml);
                         mOnReceivePendingNum.onPending(pendingNum);
                     }
                     @Override
                     public void onComplete() {
                     }
                     @Override
                     public void onError(Throwable e) {
                         CreateDialog.dismiss(activity);
                         if(null != e && null != e.getMessage()){
                             Log.e("Throwable", e.getMessage());
                             new MyToast(activity).showinfo(e.getMessage());
                         }
                     }
                 });
         addDisposable(disposable);
     }
    private OnReceivePendingNum mOnReceivePendingNum;
    public   interface OnReceivePendingNum{
        void onPending(String onPendingNum);
    }
    public void registerOnReceivePendingNum(OnReceivePendingNum onReceivePendingNum){
        this.mOnReceivePendingNum=onReceivePendingNum;
    }
}
